//
//  BBShopViewController.swift
//  BBLinkiOSProject
//
//  Created by liuzhu on 2018/11/9.
//  Copyright © 2018 liuzhu. All rights reserved.
//

import LPBannerView
import SVProgressHUD
import UIKit
private let BBShopReuseIdentifier = "BBShopReuseIdentifier" // 全局
class BBShopViewController: BBBaseViewController {
    var productList: [BBProduct]? {
        didSet {
            self.collectionView.reloadData()
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // 1.如果没有登录, 就设置未登录界面的信息
        if !userLogin {
            visitorView?.setupVisitorInfo(animationType: 3, imageName: "Visitor_shop_back", message: "Choose a favorite product!")
            return
        }
        setNav()
        view.addSubview(bannerView)
        // 注册一个cell
        collectionView.register(BBShopCollectionViewCell.self, forCellWithReuseIdentifier: BBShopReuseIdentifier)
        view.addSubview(collectionView)
        SVProgressHUD.show()
        BBProduct.loadData { _products, _ in
            SVProgressHUD.dismiss()
            self.productList = _products
        }
    }

    override func viewWillAppear(_ animated: Bool) {
        navigationController?.navigationBar.isHidden = true
        tabBarController?.tabBar.isHidden = false
    }

    override func viewDidDisappear(_ animated: Bool) {
        navigationController?.navigationBar.isHidden = false
    }

    private func setNav() {
        let navView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 90))
        let navBacImage = UIImageView(frame: CGRect(x: 0, y: 0, width: navView.bounds.size.width, height: navView.bounds.size.height))
        navBacImage.image = UIImage(named: "shop_nav_bac")
        navView.addSubview(navBacImage)
        let searchBar = UISearchBar(frame: CGRect(x: 0, y: 40, width: navView.bounds.size.width - 90, height: 40))
        let clearImg = imageWithColor(color: UIColor.clear, hight: 40)
        // 搜索框背景透明
        searchBar.backgroundImage = clearImg
        // 搜索框内部背景透明
        // searchBar.setSearchFieldBackgroundImage(clearImg, for: UIControl.State.normal)
        searchBar.backgroundColor = UIColor.clear
        navView.addSubview(searchBar)
        let orderBT = UIButton()
        orderBT.setBackgroundImage(UIImage(named: "shop_order"), for: UIControl.State.normal)
        navView.addSubview(orderBT)
        orderBT.xmg_AlignVertical(type: XMG_AlignType.CenterRight, referView: searchBar, size: CGSize(width: 35, height: 35), offset: CGPoint(x: 0, y: 0))
        let cartBT = UIButton()
        cartBT.setBackgroundImage(UIImage(named: "shop_cart"), for: UIControl.State.normal)
        navView.addSubview(cartBT)
        cartBT.xmg_AlignVertical(type: XMG_AlignType.CenterRight, referView: orderBT, size: CGSize(width: 35, height: 35), offset: CGPoint(x: 10, y: 0))
        view.addSubview(navView)
    }

    private lazy var bannerView: UIView = {
        let backView = UIView(frame: CGRect(x: 0, y: 90, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.width / 2))
        backView.backgroundColor = UIColor(red: 249.0 / 255.0, green: 245.0 / 255.0, blue: 237.0 / 255.0, alpha: 1)
        let bannerView = LPBannerView(frame: CGRect(x: 10, y: 0, width: UIScreen.main.bounds.size.width - 20, height: UIScreen.main.bounds.size.width / 2))
        bannerView.autoScrollTimeInterval = 3
        bannerView.imagePaths = ["shop_banner_1", "shop_banner_2"]
        backView.addSubview(bannerView)
        return backView
    }()

    private lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        let width = (UIScreen.main.bounds.size.width - 10) / 2
        layout.itemSize = CGSize(width: width, height: width * 1.2)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = UICollectionView.ScrollDirection.vertical

        let coll = UICollectionView(frame: CGRect(x: 0, y: bannerView.frame.origin.y + bannerView.bounds.size.height, width: UIScreen.main.bounds.size.width - 10, height: UIScreen.main.bounds.size.height - bannerView.bounds.size.height - 90), collectionViewLayout: layout)
        coll.delegate = self
        coll.dataSource = self
        coll.showsVerticalScrollIndicator = false
        coll.bounces = false
        coll.backgroundColor = UIColor.white
        return coll
    }()

    // 生成图片
    private func imageWithColor(color: UIColor, hight: Float) -> UIImage {
        let rect = CGRect(x: 0, y: 0, width: 1, height: Int(hight))
        UIGraphicsBeginImageContext(rect.size)
        let context = UIGraphicsGetCurrentContext()
        context!.setFillColor(color.cgColor)
        context!.fill(rect)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image!
    }
}

extension BBShopViewController: UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return productList?.count ?? 0
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        // 获取cell
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: BBShopReuseIdentifier, for: indexPath) as! BBShopCollectionViewCell
        // 设置cell的数据
        cell.backgroundColor = UIColor.white
        cell.product = productList![indexPath.row]
        // 返回cell
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let vc = BBProductDetailViewController()
        let product = productList![indexPath.row]
        vc.bannerList = product.showImages?.components(separatedBy: ";")
        vc.productDes = product.reserveStr1?.components(separatedBy: ";")
        navigationController?.pushViewController(vc, animated: false)
    }
}
